﻿#include "queue.h"  
#include<stdio.h>
#include<stdlib.h>

bool CreateQueue(Queue *queue, int size){
	if (size <= 0)
	return false;
	queue->values = (double*)malloc(sizeof(double)*size);
	queue->front = 0;
	queue->rear = -1;
	queue->counter = 0;
	queue->maxSize = size;
	return true;
}
//Creates an empty queue whose capacity is size

bool IsEmpty(Queue* queue){
	if(queue==NULL)
		return true;
	if(queue->counter==0)
		return true;
	return false;
}
//Returns true if the queue is empty and false otherwise

bool IsFull(Queue* queue){
	if(queue->counter==queue->maxSize)
		return true;
	return false;
}
//Returns true if the queue is full and false otherwise

bool Enqueue(Queue* queue, double x){
	if(IsFull(queue))
		return false;
	queue->rear = (queue->rear+1) % queue->maxSize;
	queue->values[queue->rear] = x;
	queue->counter++;
	return true;
}
//Adds a new element with value x to the rear of the queue
//Returns true if the operation is successful and false otherwise

bool Dequeue(Queue* queue, double* x){
	if(IsEmpty(queue))
		return false;
	*x=queue->values[queue->front];
	queue->front= (queue->front+1) % queue->maxSize;
	queue->counter--;
	return true;
}
//Removes an element from the front 
//Returns true if the operation is successful and false otherwise
//Passes the value of the front element to x

void DisplayQueue(Queue* queue){
	printf("********** DisplayQueue **********\n");
	if(IsEmpty(queue)){
		printf("Empty queue!\n");//check if the queue is empty
	      return;
      }
	int i,j;
	for(i=0;i<queue->counter;i++){
		j=(i+queue->front)%queue->maxSize;
		printf("%.2f\t",queue->values[j]);
	}//start from front position and loop counter times
	printf("\n");
}
//Prints all the elements of the queue

void DestroyQueue(Queue** pqueue){
	Queue* queue=*pqueue;
	free(queue->values);//Frees the memory occupied by the queue values
	free(queue);//Frees the memory occupied by the queue struct
	*pqueue=NULL;//Point the queue to NULL
}
//Frees the memory occupied by the queue values and queue struct
//Point the queue to NULL
